import { combineReducers } from "redux";
import {reducer as headerReducer} from '../common/header/store';
import * as actionTypes from './actionTypes';

const defaultStates = {
    roomList: [],
    bellList: [],
    bellShow: false,
    bells: 0
}

const reducer = combineReducers({
    app: (state = defaultStates,action)=>{
        switch(action.type){
            //Header.js
            case 'SHOW_BELL':
                const newState1 = JSON.parse(JSON.stringify(state));
                newState1.bellShow = true;
                return newState1;
            //App.js
            case actionTypes.SET_ROOM_LIST:
                const newState3 = JSON.parse(JSON.stringify(state));
                newState3.roomList = action.data;
                return newState3;
            case actionTypes.SET_BELL_LIST:
                const newState4 = JSON.parse(JSON.stringify(state));
                newState4.bellList = action.data;
                newState4.bells = action.data.length;
                return newState4;
            //Remind.js
            case actionTypes.REMIND_SET_BELL_LIST:
                const newState5 = JSON.parse(JSON.stringify(state));
                newState5.bellList = action.data;
                newState5.bells = action.data.length;
                return newState5;
            case actionTypes.REMIND_CLOSE_BELL:
                const newState2 = JSON.parse(JSON.stringify(state));
                newState2.bellShow = false;
                return newState2;
            case actionTypes.REMIND_GET_READED:
                const newState10 = JSON.parse(JSON.stringify(state));
                newState10.bellList[action.id].isRead = true;
                newState10.bells -= 1;
                return newState10;
            //Home.js
            case actionTypes.LIVE_IN:
                const newState6 = JSON.parse(JSON.stringify(state));
                newState6.roomList[action.id-1].used = true;
                newState6.roomList[action.id-1].number = parseInt(action.number);
                newState6.roomList[action.id-1].name = action.name;
                newState6.roomList[action.id-1].phone = action.phone;
                newState6.roomList[action.id-1].time = parseInt(action.time);
                if(parseInt(action.time)<=2){
                    newState6.bells += 1;
                    var obj = {
                        id: newState6.roomList[action.id-1].id,
                        name: newState6.roomList[action.id-1].name,
                        phone: newState6.roomList[action.id-1].phone,
                        time: newState6.roomList[action.id-1].time,
                        idRead: false
                    }
                    newState6.bellList.push(obj);
                }
                return newState6
            case actionTypes.LIVE_OUT:
                const newState7 = JSON.parse(JSON.stringify(state));
                newState7.roomList[action.id-1].used = false;
                newState7.roomList[action.id-1].name = null;
            //Money.js
            case actionTypes.ADD_TIME:
                const newState8 = JSON.parse(JSON.stringify(state));
                if(newState8.roomList[action.id-1].time<=2){
                    newState8.roomList[action.id-1].time += parseInt(action.time);
                    if(newState8.roomList[action.id-1].time>2){
                        newState8.bells -= 1;
                        for(var i=0;i<newState8.bellList.length;i++){
                            if(newState8.bellList[i].name === newState8.roomList[action.id-1].name){
                                newState8.bellList.splice(i,1);
                                return newState8;
                            }
                        }
                    }else{
                        for(var i=0;i<newState8.bellList.length;i++){
                            if(newState8.bellList[i].name === newState8.roomList[action.id-1].name){
                                const time = parseInt(newState8.bellList[i].time) + parseInt(action.time);
                                newState8.bellList[i].time = time;
                                return newState8;
                            }
                        }
                    }
                }else{
                    newState8.roomList[action.id-1].time += parseInt(action.time);
                    return newState8;
                }
            case actionTypes.SET_BALANCE:
                const newState11 = JSON.parse(JSON.stringify(state));
                newState11.roomList[action.id-1].name = '';
                newState11.roomList[action.id-1].phone = '';
                newState11.roomList[action.id-1].time = '';
                newState11.roomList[action.id-1].used = 0;
                for(var i=0;i<newState11.bellList.length;i++){
                    if(newState11.bellList[i].id === action.id){
                        newState11.bellList.splice(i,1);
                        newState11.bells -= 1;
                        return newState11;
                    }
                }
                return newState11;
            default:
                return state;
        }
    },
    header: headerReducer
})
export default reducer;